package com.momoc.multi.chat.room.core.consumer;

import com.fasterxml.jackson.core.type.TypeReference;
import com.google.protobuf.MessageLiteOrBuilder;
import com.momoc.multi.chat.room.common.dto.AuroraQueueMessageDTO;
import com.momoc.multi.chat.room.common.dto.UserOnlineCacheDTO;
import com.momoc.multi.chat.room.common.utils.JsonUtils;
import com.momoc.multi.chat.room.core.session.SessionManagerContext;
import com.momoc.netty.frame.ProtobufUtils;
import com.momoc.netty.frame.config.SmartNettyProperties;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.listener.MessageListenerContainer;
import org.springframework.amqp.rabbit.listener.RabbitListenerEndpointRegistry;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.amqp.rabbit.listener.api.ChannelAwareMessageListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author momoc
 * @version 1.0
 * @className InstanceConsumerListener
 * @description
 * @date 2024/5/28 11:19
 */
@Configuration
@RequiredArgsConstructor
public class InstanceConsumerListener {
    Logger log = LoggerFactory.getLogger(InstanceConsumerListener.class);

    final SmartNettyProperties smartNettyProperties;


    final RabbitMessageConfig rabbitMessageConfig;
    final SessionManagerContext sessionManagerContext;


    final  RabbitListenerEndpointRegistry rabbitListenerEndpointRegistry;
    final ConnectionFactory connectionFactory;

    @Bean
    public MessageListenerContainer messageListenerContainer() {
        SimpleMessageListenerContainer simpleMessageListenerContainer =
                new SimpleMessageListenerContainer();

        simpleMessageListenerContainer.setConnectionFactory(connectionFactory);
        simpleMessageListenerContainer.setQueueNames(rabbitMessageConfig.getInstanceQueueName());


        simpleMessageListenerContainer.setMessageListener(
                (ChannelAwareMessageListener) (message, channel) -> {

                    AuroraQueueMessageDTO auroraQueueMessageDTO = JsonUtils.fromJson(new String(message.getBody()), new TypeReference<AuroraQueueMessageDTO>() {
                    });
                    sendMsgToUser(auroraQueueMessageDTO);
                });

        return simpleMessageListenerContainer;
    }




    /**
     * 要发到当前实例的用户进行消息消费
     *
     * @param messageDTO
     */
    public void sendMsgToUser(AuroraQueueMessageDTO messageDTO) {

        log.debug("consumer:{}  {} {}", messageDTO.getMessageNum(),  messageDTO.getMessageBody(), messageDTO.getUserList());
        /**
         *  消息号ID
         */
        //看一下消息内容
        MessageLiteOrBuilder messageLiteOrBuilder = ProtobufUtils.decodeResponseMessage(messageDTO.getMessageBody());
        log.info("消息内容：{}", messageLiteOrBuilder.toString());

        for (UserOnlineCacheDTO userOnlineCacheDTO : messageDTO.getUserList()) {
            sessionManagerContext.sendMsgToUserByTokenWs(userOnlineCacheDTO.getTokenWs(), messageDTO.getMessageBody());
        }
    }

}
